<template>
	<view>
		<!-- 状态栏 -->
		<view v-if="showHeader" class="status"
			:style="{ position: headerPosition,top:statusTop,opacity: afterHeaderOpacity}"></view>
		<!-- 顶部导航栏 -->
		<view v-if="showHeader" class="header"
			:style="{ position: headerPosition,top:headerTop,opacity: afterHeaderOpacity}">
			<!-- 搜索框 -->
			<view class="input-box">
				<input placeholder="全网素材一搜搞定" placeholder-style="color:#c0c0c0;" @tap="toSearch()" />
				<view class="icon search"></view>
			</view>
		</view>
		<!-- 占位 -->
		<view v-if="showHeader" class="place"></view>
		<!-- 轮播图 -->
		<view class="swiper">
			<view class="swiper-box">
				<swiper circular="true" autoplay="true" @change="swiperChange">
					<swiper-item v-for="swiper in swiperList" :key="swiper.id">
						<image :src="swiper.img_url_full" @tap="toSwiper(swiper)" mode="widthFix"></image>
					</swiper-item>
				</swiper>
				<view class="indicator">
					<view class="dots" v-for="(swiper, index) in swiperList"
						:class="[currentSwiper >= index ? 'on' : '']" :key="index"></view>
				</view>
			</view>
		</view>
		<!-- 金刚区 -->
		<view class="category-list">
			<view class="category" v-for="(row, index) in services" :key="index" @tap="toCategory(row)">
				<view class="img">
					<image :src="row.img"></image>
				</view>
				<view class="text">{{ row.name }}</view>
			</view>
		</view>
		<!-- 腰部广告图 -->
		<view class="banner" v-if="ads.length > 0">
			<swiper circular="true" autoplay="true">
				<swiper-item v-for="swiper in ads" :key="swiper.id">
					<image :src="swiper.img_url_full" @tap="turnTo(swiper.link)" mode="widthFix"></image>
				</swiper-item>
			</swiper>
		</view>
		<!-- 活动区 -->
		<view class="promotion">
			<view class="text">素材优选</view>
			<view class="list">
				<view class="column" v-for="(row, index) in Promotion" @tap="toPromotion(row)" :key="index">
					<view class="top">
						<view class="title">{{row.title}}</view>
					</view>
					<view class="left">
						<view class="ad">{{row.ad}}</view>
						<view class="into">点击进入</view>
					</view>
					<view class="right">
						<image :src="row.img"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showHeader: true,
				afterHeaderOpacity: 1, //不透明度
				headerPosition: 'fixed',
				headerTop: null,
				statusTop: null,
				nVueTitle: null,
				currentSwiper: 0,
				swiperList: [],
				ads: [],
				services: [],
				Promotion: [{
					title: '文案库',
					ad: '爆款文案任你拿',
					img: this.$configData.assetsUrl + 'content.png'
				}, {
					title: '视频素材',
					ad: '热门视频随你选',
					img: this.$configData.assetsUrl + 'video.png'
				}],
				loadingText: '正在加载...',
			};
		},
		onLoad(options) {
			//获取首页配置信息
			this.getConfigData();
		},
		onShareAppMessage(res) {
			return {
				title: '爱酷您的专属工具集',
				path: '/pages/index/index',
			}
		},
		onShareTimeline(res) {
			return {
				title: '爱酷您的专属工具集',
				path: '/pages/index/index',
			}
		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			this.headerPosition = e.scrollTop >= 0 ? "fixed" : "absolute";
			this.headerTop = e.scrollTop >= 0 ? null : 0;
			this.statusTop = e.scrollTop >= 0 ? null : -this.statusHeight + 'px';
		},
		methods: {
			//获取配置信息
			async getConfigData() {
				var res = await this.$http.requestApi('GET', 'client/getHomeData');
				this.swiperList = res.data.banners;
				this.services = res.data.services;
				this.ads = res.data.ads;
			},
			//消息列表
			toMsg() {

			},
			//搜索跳转
			toSearch() {

			},
			//轮播图跳转
			toSwiper(e) {

			},
			//分类跳转
			toCategory(e) {
				if (e.appid == '') {
					uni.showToast({
						title: '敬请期待',
						icon: 'none'
					});
					return false;
				}
				if (e.appid == this.$weixinAppid) {
					//跳转本小程序
					if (e.isTabBar == 1) {
						uni.switchTab({
							url: '/' + e.page
						});
					} else {
						uni.navigateTo({
							url: '/' + e.page
						});
					}
				} else {
					//跳转其他小程序
					uni.navigateToMiniProgram({
						appId: e.appid,
						path: e.page,
						fail() {}
					})
				}
			},
			//推荐商品跳转
			toPromotion(e) {
				uni.showToast({
					title: '敬请期待',
					icon: 'none'
				});
			},
			//轮播图指示器
			swiperChange(event) {
				this.currentSwiper = event.detail.current;
			}
		}
	};
</script>
<style lang="scss">
	page {
		position: relative;
		background-color: #fff;
	}

	.pullDown-effects {
		position: fixed;
		//top: calc(100rpx - 36vw);
		top: 0;
		z-index: 9;
		width: 100%;
		height: 36vw;
		/*  #ifdef  APP-PLUS  */
		padding-top: var(--status-bar-height);

		/*  #endif  */
		image {
			width: 100%;
			height: 36vw;
		}
	}

	.status {
		width: 100%;
		height: 0;
		position: fixed;
		z-index: 10;
		background-color: #fff;
		top: 0;
		/*  #ifdef  APP-PLUS  */
		height: var(--status-bar-height); //覆盖样式
		/*  #endif  */
	}

	.header {
		width: 92%;
		padding: 0 4%;
		height: 100rpx;
		display: flex;
		align-items: center;
		position: fixed;
		top: 0;
		z-index: 10;
		background-color: #fff;

		/*  #ifdef  APP-PLUS  */
		top: var(--status-bar-height);
		/*  #endif  */

		.input-box {
			width: 100%;
			height: 60rpx;
			background-color: #f5f5f5;
			border-radius: 30rpx;
			position: relative;
			display: flex;
			align-items: center;

			.icon {
				display: flex;
				align-items: center;
				position: absolute;
				top: 0;
				right: 0;
				width: 60rpx;
				height: 60rpx;
				font-size: 34rpx;
				color: #c0c0c0;
			}

			input {
				padding-left: 28rpx;
				height: 28rpx;
				font-size: 28rpx;
			}
		}

		.icon-btn {
			width: 120rpx;
			height: 60rpx;
			flex-shrink: 0;
			display: flex;

			.icon {
				width: 60rpx;
				height: 60rpx;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				font-size: 42rpx;
			}
		}
	}

	.place {
		background-color: #ffffff;
		height: 100rpx;
		/*  #ifdef  APP-PLUS  */
		margin-top: var(--status-bar-height);
		/*  #endif  */
	}

	.swiper {
		width: 100%;
		margin-top: 10rpx;
		display: flex;
		justify-content: center;

		.swiper-box {
			width: 92%;
			height: 280rpx;

			overflow: hidden;
			border-radius: 15rpx;
			box-shadow: 0rpx 8rpx 25rpx rgba(0, 0, 0, 0.2);
			//兼容ios，微信小程序
			position: relative;
			z-index: 1;

			swiper {
				width: 100%;
				height: 280rpx;

				swiper-item {
					image {
						width: 100%;
						height: 30.7vw;
					}
				}
			}

			.indicator {
				position: absolute;
				bottom: 20rpx;
				left: 20rpx;
				background-color: rgba(255, 255, 255, 0.4);
				width: 150rpx;
				height: 5rpx;
				border-radius: 3rpx;
				overflow: hidden;
				display: flex;

				.dots {
					width: 0rpx;
					background-color: rgba(255, 255, 255, 1);
					transition: all 0.3s ease-out;

					&.on {
						width: (100%/3);
					}
				}
			}
		}
	}

	.category-list {
		width: 92%;
		margin: 0 4%;
		padding: 0 0 30rpx 0;
		border-bottom: solid 2rpx #f6f6f6;
		display: flex;
		// justify-content: space-between;
		flex-wrap: wrap;

		.category {
			width: 25%;
			margin-top: 50rpx;
			display: flex;
			flex-wrap: wrap;

			.img {
				width: 100%;
				display: flex;
				justify-content: center;

				image {
					width: 100rpx;
					height: 100rpx;
				}
			}

			.text {
				margin-top: 16rpx;
				width: 100%;
				display: flex;
				justify-content: center;
				font-size: 24rpx;
				color: #3c3c3c;
			}
		}
	}

	.banner {
		width: 92%;
		margin: 40rpx auto;
		height: 190rpx;

		image {
			width: 100%;
		}
	}

	.promotion {
		width: 92%;
		margin: 30rpx 4%;

		.text {
			width: 100%;
			height: 80rpx;
			font-size: 34rpx;
			font-weight: 600;
			margin-top: -10rpx;
		}

		.list {
			width: 100%;
			display: flex;
			justify-content: space-between;

			.column {
				width: 43%;
				padding: 15rpx 3%;
				background-color: #ebf9f9;
				border-radius: 10rpx;
				overflow: hidden;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.top {
					width: 100%;
					height: 40rpx;
					display: flex;
					align-items: center;
					margin-bottom: 5rpx;

					.title {
						font-size: 30rpx;
					}

					.countdown {
						margin-left: 20rpx;
						display: flex;
						height: 40rpx;
						display: flex;
						align-items: center;
						font-size: 20rpx;

						view {
							height: 30rpx;
							background-color: #f06c7a;
							display: flex;
							justify-content: center;
							align-items: center;
							color: #fff;
							border-radius: 4rpx;
							margin: 0 4rpx;
							padding: 0 2rpx;
						}
					}
				}

				.left {
					width: 55%;
					height: 140rpx;
					display: flex;
					flex-wrap: wrap;
					align-content: space-between;

					.ad {
						margin-top: 5rpx;
						width: 100%;
						font-size: 22rpx;
						color: #acb0b0;
					}

					.into {
						width: 100%;
						font-size: 24rpx;
						color: #aaa;
						margin-bottom: 5rpx;
					}
				}

				.right {
					width: 110rpx;
					height: 110rpx;

					image {
						width: 110rpx;
						height: 110rpx;
					}
				}
			}
		}
	}

	.goods-list {

		// background-color: #f4f4f4;
		.title {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			color: #f47825;
			font-size: 30rpx;
			margin-top: 10rpx;

			image {
				width: 30rpx;
				height: 30rpx;
			}
		}

		.loading-text {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 60rpx;
			color: #979797;
			font-size: 24rpx;
		}

		.product-list {
			width: 92%;
			padding: 0 4% 3vw 4%;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.product {
				width: 48%;
				border-radius: 20rpx;
				background-color: #fff;
				margin: 0 0 15rpx 0;
				box-shadow: 0rpx 5rpx 25rpx rgba(0, 0, 0, 0.1);

				image {
					width: 100%;
					border-radius: 20rpx 20rpx 0 0;
				}

				.name {
					width: 92%;
					padding: 10rpx 4%;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					text-align: justify;
					overflow: hidden;
					font-size: 30rpx;
				}

				.info {
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					width: 92%;
					padding: 10rpx 4% 10rpx 4%;

					.price {
						color: #e65339;
						font-size: 30rpx;
						font-weight: 600;
					}

					.slogan {
						color: #807c87;
						font-size: 24rpx;
					}
				}
			}
		}
	}
</style>